package practiseDemo;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.jmeter.samplers.AbstractSampler;
import org.apache.jmeter.samplers.Entry;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

public class practiseDemo01Sample extends AbstractSampler implements ActionListener{
	public static final Logger log=LoggingManager.getLoggerForClass();
	public static final long serialVersionUID=240L;
	private static AtomicInteger classCount=new AtomicInteger(0);
	private String b="1";
	private String a="1";
	public String getA() {
		return a;
	}


	public void setA(String a) {
		this.a = a;
	}


	public String getB() {
		return b;
	}


	public void setB(String b) {
		this.b = b;
	}





	
	
	public practiseDemo01Sample() {
		// TODO Auto-generated constructor stub
		classCount.incrementAndGet();//访问一次加一下，几个线程就有几次
		trace("parctiseDemo01Sample");
		
	}
	
	
	@Override
	public SampleResult sample(Entry arg0) {
		
		trace("parctiseDemo01Sample");
		SampleResult res=new SampleResult();
		boolean isOK=false;//判断sample是否正常运行
		String response;
		res.setSampleLabel(this.getName());
		res.sampleStart();
		try {
			int aa=Integer.valueOf(getA());
			int bb=Integer.valueOf(getB());
			response="当前线程是："+Thread.currentThread().getName()+"\r\n"+(a+b);
			res.setResponseData(response);
			res.setSamplerData("这里是samplerdata");
			res.setDataType(SampleResult.TEXT);
			res.setResponseCodeOK();
			res.setResponseMessage("程序运行正常");
			isOK=true;
			
		} catch (Exception e) {
			res.setResponseCode("500");
			res.setResponseMessage("ab都要输入int型数字啊");
			res.setResponseData(getA());
		}
		res.sampleEnd();
		res.setSuccessful(isOK);
		
		return res;
	}
	
	
	
	
	
	private void trace(String s){
		String tl=this.getName();
		System.out.println(tl);
		String tn=Thread.currentThread().getName();
		String th=toString();
		System.out.println(th);
		 log.debug(tn + " (" + classCount.get() + ") " + tl + " " + s + " " + th);
		  
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		practiseDemo01SampleGui sampleGui = new practiseDemo01SampleGui();
		setA(sampleGui.getWritea().getText());
		setB(sampleGui.getWritab().getText());
		
	}
	
}
